﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace MockCommon
{
    public sealed class UserUtility
    {
        private static Random r = new Random();

        /// <summary>
        /// Prevents a default instance of the <see cref="UserUtility"/> class from being created.
        /// </summary>
        private UserUtility()
        {
        }

        /// <summary>
        /// Generates the name of the user.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <returns></returns>
        public static string GenerateUserName(string firstName, string lastName)
        {
            lastName = Utility.RemoveMark(lastName);
            lastName = lastName.Trim();
            string fixUser = "";
            if (lastName.IndexOf(' ') != -1)
            {
                return null;
            }
            firstName = Utility.RemoveMark(firstName.Trim());
            firstName = Regex.Replace(firstName, @"\s+", " ");
            if (firstName.IndexOf(' ') != -1)
            {
                string[] fist = firstName.Split(' ');
                foreach (string begin in fist)
                {
                    fixUser += begin.Substring(0, 1);
                }
            }
            else
            {
                fixUser = firstName.Substring(0, 1);
            }

            string username = lastName + fixUser;
            return username.ToLower(CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Generates the password.
        /// </summary>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        public static string GeneratePassword(int length)
        {
            string password = "";
            int firstUpperCase = 65;
            int lastUpperCase = 91;
            int firstLowerCase = 97;
            int lastLowerCase = 123;
            int firstNumberCase = 48;
            int lastNumberCase = 58;

            int[] upperCases = { 0, 1, 3, 10 };
            int[] lowerCases = { 4, 6, 7, 9 };
            int[] numberCases = { 2, 5, 8, 11 };

            int loc = 0;
            for (int i = 0; i < length; i++)
            {
                int characterIndex = 65;
                if (upperCases.Contains(loc))
                {
                    characterIndex = r.Next(firstUpperCase, lastUpperCase);
                }
                else if (lowerCases.Contains(loc))
                {
                    characterIndex = r.Next(firstLowerCase, lastLowerCase);
                }
                else if (numberCases.Contains(loc))
                {
                    characterIndex = r.Next(firstNumberCase, lastNumberCase);
                }
                password += (char)characterIndex;
                loc++;
                if (loc == 12)
                {
                    loc = 0;
                }
            }
            return password;
        }
    }
}
